package com.qk.controller;

import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j

@RestController
public class UploadController {
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile file)throws Exception {
//        log.info("上传文件: username={}, age={}, file={}", username, age, file);
//        //构造一个文件名(不能重复)->使用uuid
//        String fileName = file.getOriginalFilename();//获取上传文件的原始文件名
//        String exName = fileName.substring(fileName.lastIndexOf("."));
//        String newFileName = UUID.randomUUID().toString() + exName;
//        file.transferTo(new File("C://tmp//"+newFileName ));
//        return Result.success();
//    }
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception{
        log.info("文件上传开始：{}", image.getOriginalFilename());
        //获取原始文件后缀名
        String originalFilename = image.getOriginalFilename();
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //生成新的文件名
        String objectName = UUID.randomUUID().toString() + suffixName;
        String url = aliyunOSSOperator.upload(image.getBytes(), objectName);
        return Result.success(url);
    }
}
